<?php

/**************************************************************************************************************************
 *                                                      === XERO ===                                                      *
 *                                                  hello.xero@gmail.com                                                  *
 * We provide a FREE software consultancy and development service to ensure that your ideas will be realized.             *
 * Gurus in C++ / C / (ARM, Z80, Intel, 68K) Assembler / LISP / LUA / Smalltalk / PHP / Java / Symbian / Windows /        *
 * Linux / Kernels / Architecture / API design / Penetration / Protection / Graphics / Audio                              *
 **************************************************************************************************************************/


/**
 * This class models the XHTML <select> element.
 * After construction it can be used to generate an XHTML string for the <select> element it represents.
 * @author XERO
 *
 */
class SelectElement
{
    /**
     * Constructor
     *
     * @param array|string $aAttrs Attributes added to the 'select' element.
     * @param array $aOptions An array of key-value pairs where the array
     *              key is the radio value, and the array value is the radio text.
     * @param mixed $aSelected The option values to mark as 'selected'; if an
     *              array, will mark all values in the array as 'selected' (used for
     *              multiple-select elements).
     * @return string The XHTML select tag.
     */
    public function __construct($aAttrs = null, $aOptions = null, $aSelected = null)
    {
        $this->_attrs = (array)$aAttrs;
        $this->_options = (array)$aOptions;
        $this->_selected = (array)$aSelected;
    }


    /**
     * Get a string representation of this XHTML <select> element.
     */
    public function __toString()
    {
        // Build the list of options
        $xhtml = '';
        if (!empty($this->_options)) {
            $options = array();
            foreach ($this->_options as $value => $group) {
                if (is_array($group)) {
                    $options[] = '<optgroup label = "' . escape($value) . '">';
                    foreach ($group as $value => $label) {
                        $options[] = $this->_createOption($label, $value);
                    }
                    $options[] = '</optgroup>';
                } else {
                    $options[] = $this->_createOption($group, $value);
                }
            }

            // Build XHTML
            $xhtml = toAttributeString($this->_attrs);
            if (strlen($xhtml) > 0) {
                $xhtml = ' ' . $xhtml;
            }
            $xhtml = "<select{$xhtml}>" . implode("", $options) . "</select>\n";
        }
        return $xhtml;
    }


    /**
     * Create the <option> element.
     *
     * @param string $aLabel Option label.
     * @param string $aValue Option value.
     * @param string $aSelected The option value to mark as selected.
     * @return string XHTML option element.
     */
    protected function _createOption($aLabel, $aValue)
    {
        $attrs = array('value' => escape($aValue));

        if (in_array($aValue, $this->_selected)) {
            $attrs['selected'] = 'selected';
        }

        return "<option " . toAttributeString($attrs) . ">{$aLabel}</option>";
    }


    protected $_attrs;
    protected $_options;
    protected $_selected;
}
